package com.face.server.main.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.face.server.common.domain.ByteArrayReceiverEx;
import com.face.server.common.domain.entity.PaperAdvert;
import com.face.server.common.domain.entity.PaperSettings;
import com.face.server.communication.mina.cache.MinaCache;
import com.face.server.main.service.FacePaperService;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author yangzhongming
 * @desc 【人脸识别出纸机-相关操作实现类】
 * @date 2022/1/24
 */
@Service
public class FacePaperServiceImpl implements FacePaperService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Boolean synchronizeAdvert(String token, PaperAdvert advert) {
        if (MinaCache.map.containsKey(token)) {
            IoSession ioSession = MinaCache.map.get(token);
            if (ioSession.isConnected()) {
                ByteArrayReceiverEx byteArrayReceiverEx = new ByteArrayReceiverEx();
                byteArrayReceiverEx.setOrder((byte) 0x01);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("advert", advert);
                byte[] data = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
                byteArrayReceiverEx.setContent(data);
                byteArrayReceiverEx.setLength(data.length);
                ioSession.write(byteArrayReceiverEx);
                logger.info("下发同步广告指令成功");
                return true;
            } else {
                // todo session不可用 - 删除 - 并修改机器状态
                MinaCache.map.remove(token);
                logger.error("该设备未连接1，无法控制");
                return false;
            }
        }
        // todo session不可用 - 删除 - 并修改机器状态
        logger.error("该设备未连接2，无法控制");
        return false;
    }

    @Override
    public Boolean deleteAdvert(String token, String advertToken) {
        if (MinaCache.map.containsKey(token)) {
            IoSession ioSession = MinaCache.map.get(token);
            if (ioSession.isConnected()) {
                ByteArrayReceiverEx byteArrayReceiverEx = new ByteArrayReceiverEx();
                byteArrayReceiverEx.setOrder((byte) 0x02);
                byte[] data = advertToken.getBytes(StandardCharsets.UTF_8);
                byteArrayReceiverEx.setContent(data);
                byteArrayReceiverEx.setLength(data.length);
                ioSession.write(byteArrayReceiverEx);
                logger.info("下发删除广告指令成功");
                return true;
            } else {
                // todo session不可用 - 删除 - 并修改机器状态
                MinaCache.map.remove(token);
                logger.error("该设备未连接1，无法控制");
                return false;
            }
        }
        // todo session不可用 - 删除 - 并修改机器状态
        logger.error("该设备未连接2，无法控制");
        return false;
    }


    @Override
    public Boolean synchronizeConfig(String token, PaperSettings paperSettings) {
        if (MinaCache.map.containsKey(token)) {
            IoSession ioSession = MinaCache.map.get(token);
            if (ioSession.isConnected()) {
                ByteArrayReceiverEx byteArrayReceiverEx = new ByteArrayReceiverEx();
                byteArrayReceiverEx.setOrder((byte) 0x03);
                JSONObject jsonObject = new JSONObject();
                //拼装 下发配置信息数据
                jsonObject.put("data", paperSettings);
                byte[] data = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
                byteArrayReceiverEx.setContent(data);
                byteArrayReceiverEx.setLength(data.length);
                ioSession.write(byteArrayReceiverEx);
                logger.info("下发配置指令成功");
                return true;
            } else {
                // todo session不可用 - 删除 - 并修改机器状态
                MinaCache.map.remove(token);
                logger.error("该设备未连接1，无法控制");
                return false;
            }
        }
        // todo session不可用 - 删除 - 并修改机器状态
        logger.error("该设备未连接2，无法控制");
        return false;
    }

}
